package mrusanov.ui.player

import javax.swing._
import mrusanov.player.Player
import java.util.Date
import net.miginfocom.swing.MigLayout
import mrusanov.util.SwingUtil
import mrusanov.localization.Localization
import mrusanov.staff.Contract
import javax.swing.table.DefaultTableModel
import scala.Some

class PlayerBigPanel(player:Player, now:Date) extends JPanel{
  setLayout(new MigLayout("fill", null, "[top]"))

  add(new PlayerGeneralInfoPanel(player, now), "align center")

  add(new PlayerShapeInfoPanel(player), "wrap, align center")

  add(new PlayerPsychologyPanel(player), "align center")

  add(new PlayerContractInfoPanel(now, player), "wrap, align center")

  // TODO add nice borders and captions
}

private class PlayerGeneralInfoPanel(player:Player, now:Date) extends JPanel {
  setLayout(new MigLayout("fill"))

  add(new PlayerImage(player), "span 2, wrap")
  SwingUtil.addLabel(this, "roster.table.name", player.name)
  SwingUtil.addLabel(this, "roster.table.playerType", player.playerType.name)
  SwingUtil.addLabel(this, "roster.table.level", player.level)
  SwingUtil.addLabel(this, "roster.table.age", player.age(now))
  SwingUtil.addLabel(this, "roster.table.talent", player.talent.percentage)

}

private class PlayerShapeInfoPanel(player:Player) extends JPanel {
  setLayout(new MigLayout("fill"))

  SwingUtil.addLabel(this, "roster.table.physical", player.physicalShape)
  SwingUtil.addLabel(this, "playerPanel.hp", player.hp)
  SwingUtil.addLabel(this, "playerPanel.damage", player.damage)
  SwingUtil.addLabel(this, "playerPanel.damageType", Localization(player.playerType.damageType.messageKey))

  SwingUtil.addSingleLabel(this, "playerPanel.defences")
  player.playerType.defence.foreach (kv => {
    SwingUtil.addLabel(this, kv._1.messageKey, kv._2.toString + "%")
  })

  SwingUtil.addSingleLabel(this, "playerPanel.resistances")
  player.playerType.resistances.resistances.foreach(kv => {
    SwingUtil.addLabel(this, kv._1.messageKey, kv._2)
  })
}

private class PlayerContractInfoPanel(now:Date, player:Player) extends JPanel {
  setLayout(new MigLayout("fill"))

  player.contract match {
    case Some(cont) => addComponentsForContract(now, cont)
    case None => addComponentsForNoContract()
  }

  private def addComponentsForNoContract() {
    SwingUtil.addSingleLabel(this, "playerPanel.noContract")
  }

  private def addComponentsForContract(now:Date, contract:Contract) {
    SwingUtil.addLabel(this, "playerPanel.contractForYears", contract.moneyPerYear.size)
    SwingUtil.addLabel(this, "playerPanel.contractYearsLeft", contract.contractYearsLeft(now))
    SwingUtil.addLabel(this, "playerPanel.currentSalary", contract.salaryInYear(now))

    val salariesTable = new JTable()
    val scrollPanel = new JScrollPane(salariesTable)
    val columnsKeys = List("playerPanel.year", "playerPanel.salary")

    add(scrollPanel, "grow, push")

    val salariesTableModel = new DefaultTableModel() {
      override def isCellEditable(row:Int, column:Int) = false
    }

    columnsKeys foreach(key => salariesTableModel.addColumn(Localization(key)))

    val salariesList = convertYearsSalariesToArraysList(contract.yearAndSalaryMap)
    salariesList.foreach(salariesTableModel.addRow(_))
    salariesTable.setModel(salariesTableModel)
  }



  private def convertYearsSalariesToArraysList(map:Map[Int,Int]) = map.toList.map(p => Array[AnyRef](p._1.toString, p._2.toString))

}

private class PlayerPsychologyPanel(player:Player) extends JPanel {
  setLayout(new MigLayout("fill"))

  private val psy = player.psychology

  SwingUtil.addLabel(this, "playerPanel.ambition", psy.ambition)
  SwingUtil.addLabel(this, "playerPanel.conflict", psy.conflict)
  SwingUtil.addLabel(this, "playerPanel.discipline", psy.discipline)
  SwingUtil.addLabel(this, "playerPanel.talkative", psy.talkative)
  SwingUtil.addLabel(this, "playerPanel.worldview", psy.worldView)

  val friendsLabel = new JLabel(Localization("playerPanel.friends"))
  val enemiesLabel = new JLabel(Localization("playerPanel.enemies"))

  add(friendsLabel)
  add(enemiesLabel, "wrap")
  addList(psy.friends.map(_.name))
  addList(psy.enemies.map(_.name))

  private def addList(contents:Iterable[String]) {
    val list = new JList()
    val model = new DefaultListModel()
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION)
    contents foreach( model.addElement(_))
    list.setModel(model)
    add(list, "align center")
  }

}